package com.geostar.constructionland.analyse.service.spatiaanalyst.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.common.dao.LogMongoSptDao;
import com.geostar.constructionland.common.entity.analyse.AnalyseVersion;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.common.entity.logs.LogSpt;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.common.entity.logs.mongolog.LogMongoSpt;
import com.geostar.constructionland.common.mapper.logs.LogSptMapper;
import com.geostar.constructionland.analyse.service.AlyVerService;
import com.geostar.constructionland.analyse.service.HttpAPIService;
import com.geostar.constructionland.analyse.service.spatiaanalyst.ISpatialAnalyst;
import com.geostar.constructionland.common.service.logs.LogSptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author yelll
 * @since 2020-5-27 14:51
 * 通用的占压分析
 **/
@Slf4j
@Component
public abstract class NormalSptAnalyst implements ISpatialAnalyst {
    @Autowired
    AlyVerService alyVerService;
    @Autowired
    HttpAPIService httpAPIService;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    LogSptService logSptService;
    /***
     * 获取接口查询参数
     * @param analyseModel 分析model
     * @return 分析参数
     */
    public abstract Map<String,Object> getQueryParam(AnalyseModel analyseModel) throws Exception;


    // 新建事务，如果当前存在事务，把当前事务挂起。
    @Override
    public HttpResult sptAnalyse(AnalyseModel analyseModel) throws Exception{
        /****设置日志 step1 开始****/
        LogSpt logSpt = new LogSpt();
        logSpt.setId(UUID.randomUUID().toString().replaceAll("-",""));
        logSpt.setFxbs(analyseModel.getAlyId());
        /****设置日志 step1 结束****/

        HttpResult httpResult = null;
        /**获取分析版本*/
        AnalyseVersion analyseVersion = alyVerService.getAlyVerByAnalyModel(analyseModel);
        logSpt.setFxlx(analyseVersion.getAnalyseType());
        /**获取参数*/
        Map<String,Object> analyseParam = getQueryParam(analyseModel);

        /****设置日志 step2 开始****/
        Date startTime = new Date();
        logSpt.setStartTime(startTime);
//        logSpt.setVersion(analyseVersion.getVersionNo());
        LogMongoSpt logMongoSpt = new LogMongoSpt();
        String logMongSptId = UUID.randomUUID().toString().replaceAll("-","");
        logSpt.setMongoId(logMongSptId);
        logMongoSpt.setId(logMongSptId);
        logMongoSpt.setInput(analyseParam.get("requestInfo").toString());
        /****设置日志 step2 结束****/

        log.info("执行分析,分析类型为【"+analyseVersion.getAnalyseType()+"】,分析名【"+analyseVersion.getAnalyseName()+"】");
        try {
            httpResult = httpAPIService.doPost(analyseVersion.getUrl(), analyseParam);
        }finally {
            if(httpResult!=null){
                /**未成功**/
                logMongoSpt.setOutput(httpResult.getBody());
            }
            logSpt.setCostTime(System.currentTimeMillis()-startTime.getTime());
            logSpt.setLogMongoSpt(logMongoSpt);
            /**保存日志**/
            logSptService.saveLogSpt(logSpt);
        } 
        return httpResult;
    }

}
